<?php
require_once '../bootstrap.php';
include_once '../includes/formvalidator.php';

// Must login
if (empty($_SESSION['admin_id'])) {
	header('Location: login.php');
}

$__page = "admin_setup";
$self = $_SERVER['PHP_SELF'];
// Variables

//-----------------------------------------------------------------------
// Handling Form Submit
if (isset($_POST['Submit'])) {
	// Validation array
	$validations = array(
		'username' => array('type' => 'alfanum', 'message' => 'You must enter a valid admin username (Alpha Numeric)'),
		'password' => array('type' => 'alfanum', 'message' => 'You must enter a valid password (Alpha Numeric)'),
		'email' => array('type' => 'email', 'message' => 'You must enter a valid email address for admin'),
		'senderName' => array('type' => 'alfanum', 'message' => 'You must enter a valid Sender Name (Alpha Numeric)'),
		'senderEmail' => array('type' => 'email', 'message' => 'You must enter a valid email address for sender'),
		'rowsPerPage' => array('type' => 'number', 'message' => 'You must enter a valid rows number to show in result page'),
		'adsPerPage' => array('type' => 'number', 'message' => 'You must enter a valid ads number to show in result page'),
	);
	// Required array
	$required = array('username', 'password', 'email', 'senderName', 'senderEmail', 'rowsPerPage', 'adsPerPage'
	);
	// Loop validation array to build sanatize array
	$sanatize = array();
	foreach ($validations as $item_name => $options) {
		switch ($options['type']) {
			case 'anything':
				array_push($sanatize, $item_name);
				break;
			case 'number':
				$sanatize[$item_name] = 'int';
				break;
			case 'price':
				$sanatize[$item_name] = 'float';
				break;
			case 'email':
				$sanatize[$item_name] = 'email';
				break;
			default:
				break;
		}
	}

	$validator = new FormValidator($validations, $required, $sanatize);

	if ($validator->validate($_POST)) {
		// Very important!!
		$_POST = $validator->sanatize($_POST);

		$data = array($_POST['email'], $_POST['username'], $_POST['password'], $_POST['senderName'], $_POST['senderEmail'], $_POST['rowsPerPage'], $_POST['adsPerPage']);
		$record = $my_db->execute('UPDATE admins SET email=?, username=?, password=?, fromName=?, fromEmail=?, rowsPerPage=?, adsPerPage=?', $data);

		if ($record > 0) {
			$mess = getADMSuccess("Admin account settings have been updated.");
		} else {
			$mess = getADMSuccess("Nothing changed.");
		}
	} else {
		$form_errors = $validator->getError();

		if (!empty($form_errors)) {
			foreach ($form_errors as $error) {
				$errors .= li($error);
			}
			$mess = getADMError($errors);
		}
	}
}

if (!empty($_POST)) {
	$password = stripslashes($_POST['password']);
	$email = stripslashes($_POST['email']);
	$username = stripslashes($_POST['username']);
	$sender_name = stripslashes($_POST['senderName']);
	$sender_email = stripslashes($_POST['senderEmail']);
	$rowsPerPage = stripcslashes($_POST['rowsPerPage']);
	$adsPerPage = stripcslashes($_POST['adsPerPage']);
} else {
	// gather user information
	$admin = $my_db->select_row('SELECT * FROM admins WHERE id = ?', array($_SESSION['admin_id']));

	if (!empty($admin)) {
		$password = stripslashes($admin['password']);
		$email = stripslashes($admin['email']);
		$username = stripslashes($admin['username']);
		$sender_name = stripslashes($admin['fromName']);
		$sender_email = stripslashes($admin['fromEmail']);
		$rowsPerPage = stripcslashes($admin['rowsPerPage']);
		$adsPerPage = stripcslashes($admin['adsPerPage']);
	}
}
?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
	"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">

<head>
	<meta http-equiv="X-UA-Compatible" content="IE=7"/>
	<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-2"/>
	<title><?php echo $title; ?></title>

	<style type="text/css" media="all">
		@import url("css/style.css");
		@import url("css/jquery.wysiwyg.css");
		@import url("css/facebox.css");
		@import url("css/visualize.css");
		@import url("css/date_input.css");
	</style>

	<!--[if lt IE 8]>
	<style type="text/css" media="all">@import url("css/ie.css");</style><![endif]-->
	<script language="javascript" src="includes/functions.js"></script>

</head>


<body>

<div id="hld">

	<div class="wrapper">        <!-- wrapper begins -->

		<?php include("__menu.php");?>
		<!-- #header ends -->
		<!-- .block ends -->
		<div class="block">
			<div class="block_head">
				<div class="bheadl"></div>
				<div class="bheadr"></div>
				<h2>admin setup</h2>
			</div>
			<!-- .block_head ends -->
			<div class="block_content">
				<?php echo $mess;?>
				<form action="<?php echo $self;?>" method="post" name="update_job">
					<table width="100%" border="0" cellspacing="0" cellpadding="0">
						<tr>
							<td><strong>Name as Email Sender</strong></td>
							<td><input type="text" class="text small" name="senderName" id="sender"
							           value="<?= (!empty($sender_name)) ? $sender_name : '' ?>" maxlength="100"/></td>
						</tr>
						<tr>
							<td><strong>Email of Email Sender</strong></td>
							<td><input type="text" class="text medium" name="senderEmail" id="senderemail"
							           value="<?= (!empty($sender_email)) ? $sender_email : '' ?>" maxlength="100"/>
							</td>
						</tr>
						<tr>
							<td width="20%"><strong>Admin Username </strong></td>
							<td width="80%"><input type="text" class="text small" name="username" id="adminun"
							                       value="<?= (!empty($username)) ? $username : '' ?>" maxlength="50"/>
							</td>
						</tr>
						<tr>
							<td><strong>Admin User Password</strong></td>
							<td><input type="text" class="text small" name="password" id="adminps"
							           value="<?= (!empty($password)) ? $password : '' ?>" maxlength="50"/></td>
						</tr>
						<tr>
							<td width="20%"><strong>Admin User Email</strong></td>
							<td width="80%"><input type="text" class="text medium" name="email" id="aemail"
							                       value="<?= (!empty($email)) ? $email : '' ?>" maxlength="100"/></td>
						</tr>
						<tr>
							<td valign="top"><strong>Sports / Rows per page on Sports result page</strong></td>
							<td valign="top">
								<input type="text" name="rowsPerPage" class="text small"
								       value="<?= (!empty($rowsPerPage)) ? $rowsPerPage : 10 ?>"/>
							</td>
						</tr>
						<tr>
							<td valign="top"><strong>Ads per page on Sports result page</strong></td>
							<td valign="top">
								<input type="text" name="adsPerPage" class="text small"
								       value="<?= (!empty($adsPerPage)) ? $adsPerPage : 10 ?>"/>
							</td>
						</tr>
						<tr>
							<td valign="top">&nbsp;</td>
							<td valign="top">
								<input name="Submit" type="submit" class="submit mid" value=" Update "/>
								<input name="Reset" type="reset" class="submit mid" value=" Reset "/>
								<br/></td>
						</tr>
					</table>
				</form>
			</div>
			<!-- .block_content ends -->
		</div>
		<!-- .block ends -->
		<!-- .block ends -->
		<!-- .block.small.left ends -->
		<!-- .block.small.right ends -->
		<!-- .block ends -->
		<!-- .block ends -->
		<!-- .leftcol ends -->
		<?php include("__footer.php"); ?>
	</div>
	<!-- wrapper ends -->

</div>
<!-- #hld ends -->
<?php include("__footer_js.php"); ?>
</body>
</html>